import java.util.*;

/**
 * 连续K元素回文索引
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param nums int整型一维数组 
     * @param k int整型 
     * @return int整型一维数组
     */
    public int[] find_palindrome_subarrays (int[] nums, int k) {
        // write code here
        // [1,2,1,2,1],3
        // [0,1,2]
        // 先合成一个String
        int len = nums.length;
        StringBuilder str;
        List<Integer> retList = new ArrayList<>();
        for (int i = 0; i + k <= len; i++) {
            str = new StringBuilder();
            for (int j = 0; j < k; j++) {
                str.append(nums[i + j]);
            }
            if (isPal(String.valueOf(str))) {
                retList.add(i);
            }
        }
        int[] ret = new int[retList.size()];
        for (int i = 0; i < retList.size(); i++) {
            ret[i] = retList.get(i);
        }
        return ret;
    }

    public boolean isPal(String s) {
        int l = 0, r = s.length() - 1;
        boolean flag = true;
        while (l < r) {
            if (s.charAt(l++) != s.charAt(r--)) {
                return false;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        // [1,2,1,2,1],3
        // 12, 3, 21, 12, 3,21
        System.out.println(Arrays.toString(solution.find_palindrome_subarrays(new int[]{12, 3, 21, 12, 3,21}, 3)));
    }
}